我试图用Jquery在我的搜索表单上隐藏一个字段,这样如果它是一个商业地产,就没有‘卧室数量’字段。
jQuery(function ($) {
$(window).load(function() {
$("input:radio#comms").click(function() {
$("p.bedrooms").hide();
});
$("input:radio#sale").click(function() {
$("p.bedrooms").show();
});
$("input:radio#rent").click(function() {
$("p.bedrooms").show();
});
});
});我是用Wordpress写的,上面的代码看起来还不错,但我的问题是--如果你隐藏了一个字段,URL就会变成bedrooms=1&location=blackpool --它默认的值是1。我想知道在表单中是否有一种方法可以有一个null值,如果它隐藏了,就根本不搜索那个字段?
发布于 2012-10-08 22:08:24
您可以随时尝试将.submit()事件处理程序附加到窗体。
我还没有测试或运行下面的代码。
// Form will submit after this method runs unless you e.preventDefault();
$("#myForm").submit(function(e) {
if ($("p.bedrooms").is(":visible")) {
// Do somethin
} else {
// Bedrooms aren't visible
// Option 1: empty the value
$("input[name=bedrooms]").val("")
/*
Option 2: remove the input
Since the form is submitting, it shouldn't matter
if you remove an input. Returning to the page should
replace the input.
*/
$("input[name=bedrooms]").remove()
}
});https://stackoverflow.com/questions/12783169
复制相似问题