2010-05-09 88 views
1

, 我想创建一个简单的函数来重置所有输入值而无需目标输入。 我不知道我该怎么做。 谢谢我想创建一个简单的函数来重置没有目标输入的所有输入值。 (Javascript,Jquery)

编辑:该函数重置所有的输入值。我需要保存/捕获目标值。

这里是我的示例代码:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
<script> 
function reset_other_inputs(room) { 
$("input[name^='check_in_date_']").each(function() { 
    if ($("input[name!='check_in_date_'+room]")) { $(this).val(""); } 
}); 
$("input[name^='check_out_date_']").each(function() { 
    if ($("input[name!='check_out_date_'+room]")) { $(this).val(""); } 
}); 
} 
</script> 

<input type="text" name="check_in_date_single" value="single" onClick="reset_other_inputs('single');"> 
<input type="text" name="check_out_date_single" value="single" onClick="reset_other_inputs('single');"> 

<input type="text" name="check_in_date_double" value="double" onClick="reset_other_inputs('double');"> 
<input type="text" name="check_out_date_double" value="double" onClick="reset_other_inputs('double');"> 

<input type="text" name="check_in_date_triple" value="triple" onClick="reset_other_inputs('triple');"> 
<input type="text" name="check_out_date_triple" value="triple" onClick="reset_other_inputs('triple');"> 

<input type="text" name="check_in_date_suite" value="suite" onClick="reset_other_inputs('suite');"> 
<input type="text" name="check_out_date_suite" value="suite" onClick="reset_other_inputs('suite');"> 
+0

那么,什么样的代码部分不起作用,是什么问题? – 2010-05-09 13:53:43

+0

该功能不起作用,我无法修复该问题。 – 2010-05-09 13:55:21

+0

该函数重置所有输入值。我需要保存/捕获目标值。 – 2010-05-09 14:36:16

回答

0

好的!最后我找到了解决办法;

function reset_other_inputs(room) { 
$("input[name^='check_in_date_']").each(function() { 
    if ($(this).attr("name") != "check_in_date_"+room) { $(this).val(""); } 
}); 
$("input[name^='check_out_date_']").each(function() { 
    if ($(this).attr("name") != "check_out_date_"+room) { $(this).val(""); } 
}); 
} 

谢谢您的回答。

2

你可以做到这一切在jQuery的,不一定是简单的,但有点清洁的整体,就像这样:

$(function() { 
    $("input[name^='check_in_date_'], input[name^='check_out_date_']").click(function() { 
    var room = $(this).attr("name"); 
    room = room.substring(room.lastIndexOf("_") + 1); 
    $("input[name='check_in_date_" + room +"'], input[name='check_out_date_" + room +"']").not(this).val(''); 
    }); 
})​​​​​​;​ 

删除您的内嵌onClick此方法的处理程序,you can see a working demo here

+0

我想清除其他值,而不是目标值。 – 2010-05-09 14:08:26

+0

@question_about_the_problem - 对不起,您的原始代码确实:)更新了答案和示例演示,不清除您点击的那个。 – 2010-05-09 14:10:19

0

,似乎工作其实我要建议此解决方案:

var $relevantInputs = $('input[name^=check_in_date]') 
        .add('input[name^=check_out_date]'); 
$relevantInputs 
    .click(function() { 
     var $thisInputsValue= $(this).val(); 
     reset_other_types($thisInputsValue); 
    }); 

function reset_other_types(type) { 
    $relevantInputs 
     .filter(':not([value=' + type + '])') 
      .val("") 
     .end(); 
} 

前两行抢相关投入(如果有许多页)。第二行将click处理程序应用于所有相关输入(请记住,jQuery使用隐式迭代,因此单击处理程序将应用于上面两行中匹配的所有对象)。在点击处理程序中(用户点击了一个输入),我们得到用户点击的输入值并将其传递给reset_other_types函数。此功能基本上重置与参数type中给出的值不匹配的所有相关输入的值。迪斯科!可读,不显眼的解决方案(这意味着您不必在HTML:P中维护所有那些单独的点击处理程序)。干杯!

相关问题