2012-08-30 24 views
0

我在Objective-C中制作Mac应用程序,我需要它能够在网站上提交表单(当然,按下按钮),但所有用户都有要做的是提供与表格相关的时间。从Mac Cocoa应用程序提交HTML表单

下面是从网站的代码:

<td class=t_h>07:16</td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td class=t_s height='24'> 
<form method="POST" action="members_booking.php?operation=member_booking_form" onSubmit="return ValidateMemberBookNow(this)"> 
<INPUT NAME=double_click TYPE=hidden> 
<INPUT NAME=course_id TYPE=hidden VALUE="1"> 
<INPUT NAME=unique_id TYPE=hidden VALUE="230693"> 
<INPUT NAME=d_date TYPE=hidden VALUE="2012-09-07"> 
<INPUT NAME=Booking_Operation TYPE=hidden VALUE="Book Casual"> 
<INPUT NAME=SubmitButton TYPE=submit VALUE="Book Now"> 
</form> 
</td> 
</tr> 
<tr> 
<td class=t_h>07:24</td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td class=t_s height='24'> 
<form method="POST" action="members_booking.php?operation=member_booking_form" onSubmit="return ValidateMemberBookNow(this)"> 
<INPUT NAME=double_click TYPE=hidden> 
<INPUT NAME=course_id TYPE=hidden VALUE="1"> 
<INPUT NAME=unique_id TYPE=hidden VALUE="230694"> 
<INPUT NAME=d_date TYPE=hidden VALUE="2012-09-07"> 
<INPUT NAME=Booking_Operation TYPE=hidden VALUE="Book Casual"> 
<INPUT NAME=SubmitButton TYPE=submit VALUE="Book Now"> 
</form> 
</td> 
</tr> 
<tr> 
<td class=t_h>07:32</td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td class=t_s height='24'> 
<form method="POST" action="members_booking.php?operation=member_booking_form" onSubmit="return ValidateMemberBookNow(this)"> 
<INPUT NAME=double_click TYPE=hidden> 
<INPUT NAME=course_id TYPE=hidden VALUE="1"> 
<INPUT NAME=unique_id TYPE=hidden VALUE="230695"> 
<INPUT NAME=d_date TYPE=hidden VALUE="2012-09-07"> 
<INPUT NAME=Booking_Operation TYPE=hidden VALUE="Book Casual"> 
<INPUT NAME=SubmitButton TYPE=submit VALUE="Book Now"> 
</form> 
</td> 
</tr> 
<tr> 

最多可以有这些块的40。 <td class=t_h>07:32</td>是用户指定的时间。我需要与该时间关联的按钮自动按下。怎么样?

在此先感谢你们!

回答

1

这是jQuery进入自己的问题。基本思路是找到类t_h的元素07:32。然后,在其父节点中,找到一个form元素和submit()它。

在这里你去:

function submitFormForTime(time){ 
    $(".t_h").each(function(i, obj){ // find each element with the class t_h 
     if ($(this).text() != time) return; // not the right one 
     $(this).parent().find("form").each(function(i, obj){ 
      obj.submit(); // submit that form 
     }); 
    }); 
} 

submitFormForTime("07:32"); 

添加这个脚本的网站。要呼叫的Objective-C见这个脚本:

从上述文件,下面将调用来自Objective-C的JavaScript函数:

id win = [webView yourWebView]; // assumes your webView is called yourWebView 
[win evaluateWebScript:@"submitFormForTime('07:32');"]; 
+0

谢谢你真棒回答!对此,我真的非常感激。经过一整小时的研究和试验和错误之后,我仍然非常困惑,应该如何从Objective-C代码中调用它。我不是javaScript buff,但是从我在Apple文档中看到的内容,没有关于使用变量调用完整函数的信息(如本例中的时间)。任何更多的指针在正确的方向将非常感激。非常感谢。 –

+1

不客气,康纳。关于从Objective-C调用JavaScript,请参阅链接文档末尾的示例。我已经用示例代码更新了答案。 –

+0

谢谢,但请回答最后一个问题:使用time变量调用函数没有问题,但我该如何实现函数呢?我已经在我的资源中创建了一个单独的.js文件,但是如何使这与webView中显示的当前页面交互?再次感谢 –