快捷方式,试试这个 fiddle : http://jsfiddle.net/purushagovinda/VJfkx/
<小时/>我有一个 jquery 代码块,我想正确处理它,以便它设置隐藏输入元素的值(从 value="keywords"
更改为 value="家具”
),然后THEN调用一个函数(“refresh_itemListerWithSearchResultsReturn();
”),该函数将主 div 的内容从 ajax 调用重新加载到另一个文件 - a文件取决于隐藏输入的值。
我的代码现在成功设置了隐藏输入的值,然后触发该函数,但是,触发后,该函数仍然认为隐藏输入值=“关键字”。这让我觉得我想这样做:
$("#myHiddenInput").val('furniture');
...但是该行 ^^^ 有一个回调函数,并且我将 refresh_itemListerWithSearchResultsReturn()
函数调用粘贴在 .val() 回调内部。所以,作为一个相对菜鸟,我在这里查找:
...但没有看到如何将 .val();
与回调函数一起使用。该文档页面上的示例显示了带有函数调用的 .val()
,但该函数用于确定 val()
将用于值本身,而不是触发回调,就像我需要的那样。
对我如何进行有什么建议吗?
<小时/>代码实际上是这样的:
$("#products_furniture_furnitureType_options > li").each(function() {
$(this).click(function(e) {
[snip]
$("#dp_searchForWhichItemType_listRefresh").val('products');
$('#bit_products_productType').val('furniture');
$("#bit_products_productType").val("furniture").change(function(){
alert("'#bit_products_productType' is changed now 2");
$.refresh_itemListerWithSearchResultsReturn('itemList', false, false);
});
[snip]
});
});
我也尝试过这个:
$("#products_furniture_furnitureType_options > li").each(function() {
$(this).click(function(e) {
[snip]
$("#dp_searchForWhichItemType_listRefresh").val('products');
$('#bit_products_productType').val('furniture');
$("#bit_products_productType").change(function(){
alert("'#bit_products_productType' is changed now 2");
$.refresh_itemListerWithSearchResultsReturn('itemList', false, false);
});
[snip]
});
});
...还尝试将 $("#bit_products_productType").change(function(){
移至 $('#bit_products_productType').val('furniture ');
...
...但在任何情况下 alert("'#bit_products_productType' ischanged now 2");
都不会触发。
这应该可以帮助你们集中注意力,并且能够帮助我: http://jsfiddle.net/purushagovinda/VJfkx/
最佳答案
你可以这样做
$("#myHiddenInput").val("product").change(function(){
alert("value is changed now");
});
检查:.change() - 将事件处理程序绑定(bind)到“更改”JavaScript 事件,或在元素上触发该事件。
关于jquery如何仅在更新隐藏输入值后运行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15563799/