Javascript 调用controlgroup.click()方法
正在尝试在另一个方法中调用控件组的.click方法。以下是我到目前为止的情况:Javascript 调用controlgroup.click()方法,javascript,jquery,cordova,click,Javascript,Jquery,Cordova,Click,正在尝试在另一个方法中调用控件组的.click方法。以下是我到目前为止的情况: $("#list").click(function() { searchclick = true; value = $("#listval").text().split(" - "); window.location.href = "index.html"; item = $("#listval").attr("title"); if (item == "this") {
$("#list").click(function() {
searchclick = true;
value = $("#listval").text().split(" - ");
window.location.href = "index.html";
item = $("#listval").attr("title");
if (item == "this") {
$("#this").click();
} else {
$("#that").click();
}
});
这和那是控制组中的两个按钮。控制组单击方法的初始化方式如下:
$("#this, #that").click(function(){ ...code... }
有什么帮助吗
编辑:以下是html的外观:
<div data-role="content" class="ui-content" role="main">
<div id="item" data-role="fieldcontain">
<fieldset data-role="controlgroup" data-type="horizontal">
<legend>
</legend>
<input id="this" name="choose" value="val1" type="radio"/>
<label for="this">
val1
</label>
<input id="that" name="choose" value="val2" type="radio"/>
<label for="that">
val2
</label>
</fieldset>
</div>
...
这将把当前值发送到服务器,我想您在那里有处理发送的值并生成正确响应的代码 注: searchclick和value是全局变量吗?若是这样的话,它们将在这个点击和那个点击的处理程序中可用,但在提交表单后将丢失。 项是局部变量吗?如果是这样,您应该这样声明:var item=$listval.attrtitle;。作为一般规则:除非绝对必要,否则不要创建全局变量。 下一个代码可能会干扰表单中的其他代码。如果修复其他代码太难,您可以尝试其他选项:不要提交表单,而是执行ajax调用,请参阅jQuery文档。
最后,我将变量设置为局部变量,并在index.html页面中调用它们。在index.html中,我使用了一个try,catch块来检查页面加载时,局部变量中是否存在任何变量。单击另一页上的列表后,下面是我在index.html中的代码:
try{
//grab variables set in search
if(window.localStorage.getItem("searchclick") == "true"){
searchclick = true;
}else{searchclick = false}
thisthat = window.localStorage.getItem("thisthat");
if(thisthat == "this"){
$("#this").click();
$("#thislabel").addClass("ui-btn-hover-c");
$("#thislabel").addClass("ui-radio-on");
$("#thislabel").addClass("ui-btn-active");
}else if(thisthat == "that"){
$("#that").click();
$("#thatlabel").addClass("ui-btn-hover-c");
$("#thatlabel").addClass("ui-radio-on");
$("#thatlabel").addClass("ui-btn-active");
}
//clear local variables after being used
window.localStorage.clear();
}catcherr{alerterr;}这些单击函数从未被调用,这是您的问题吗?是的,我已经在if语句中使用警报进行了测试,以确保它们正常工作,并且它们确实有效。唯一的问题是.click方法没有启动。使用window.location.href,您将重定向到其他页面,因此该行下面的代码永远不会运行。您是否在click处理程序中放置了警报以证明它们没有运行?@keune-在window.location.href之后放置警报,它将正常启动。也试着把它移到后面,但还是什么都没有。我在同一个.js文件中有index.html和search.html的所有方法。所以它们都共享变量,并且应该保存状态,对吗?Barmar-我在方法的开头放了一个警报,看看它是否会触发,但它不会。我正在为一个移动应用程序开发这个。我计划通过PhoneGap运行此功能。无论如何,该方法将使用this和that click方法调用适当的xml数据。在此之后,将生成一个包含xml文件中所有对象名称的列表。然后根据这个,点击的值,值[0]将是它需要从该列表中选择的对象。然后,我将为该列表运行click方法并生成第二个列表。值[1]将是在第二个列表中选择的值,在使用该列表的单击方法时,我将显示结果。/\n只是尝试显示变量如何仅在方法中使用,因此它们实际上不需要传输,因为它们都在同一个.js文件中。请注意,我还没有添加更改列表等的代码。一步一个脚印。
try{
//grab variables set in search
if(window.localStorage.getItem("searchclick") == "true"){
searchclick = true;
}else{searchclick = false}
thisthat = window.localStorage.getItem("thisthat");
if(thisthat == "this"){
$("#this").click();
$("#thislabel").addClass("ui-btn-hover-c");
$("#thislabel").addClass("ui-radio-on");
$("#thislabel").addClass("ui-btn-active");
}else if(thisthat == "that"){
$("#that").click();
$("#thatlabel").addClass("ui-btn-hover-c");
$("#thatlabel").addClass("ui-radio-on");
$("#thatlabel").addClass("ui-btn-active");
}
//clear local variables after being used
window.localStorage.clear();