在一个选择上更改不同的输入值

时间:2013-01-10 22:14:23

标签: jquery

我想在一个select - 选项上获得不同的输入值。 只有我不能让它发挥作用..

$("#theSelect").change(function() {
    if($("#theSelect") == "foo")    {
      someInput1.show();
      someInput1.value = "Some Foo text";

      someInput2.show();
      someInput2.value = "different Text";
    }

有人有想法吗?

见: http://jsfiddle.net/853GJ/2/

4 个答案:

答案 0 :(得分:0)

您正在将jquery objectstring值进行比较:

if($("#theSelect") == "foo")   

您需要使用.val()获取价值:

if($("#theSelect").val() == "foo")

此外,您需要获取隐藏输入的jquery对象:

var someInput1 = $('#someInput1');

答案 1 :(得分:0)

您需要获取select元素使用val方法的值。您也可以在this内使用if来获取当前元素的值。您还需要找到输入元素以显示它们并更改其值。

$("#theSelect").change(function() {
  if($(this).val() == "foo") {
    $("#someInput1").show();
    $("#someInput1").val("Some Foo text");

    $("#someInput2").show();
    $("#someInput2").val("different Text");      
  }
});

答案 2 :(得分:0)

我在你的jquery中注意到的一些事情:

<强>的jQuery

var $someInput1 =  $("#someInput1");
var $someInput2 =  $("#someInput2");

$someInput1.hide();
$someInput2.hide();

$("#theSelect").change(function() {  

    if($("#theSelect").val() === "foo"){
      $someInput1.show().val("Some Foo text");
      $someInput2.show().val("different Text");
    }

    if($("#theSelect").val() === "bar")    {
      $someInput1.show().val("Some bar text");
      $someInput2.show().val("Text....");
    }

}).change(); // trigger once if needed
  • if($("#theSelect") == 'foo'已更改为if($("#theSelect").val() == 'foo'
  • .value已更改为val()
  • $("#someInput1")$("#someInput2")创建了缓存变量。

<强> EXAMPLE

答案 3 :(得分:0)

在变更处理程序this内引用元素。

在if中,您想要将value与可以使用val()方法检索的字符串进行比较

$("#theSelect").change(function() {
    if($(this).val() == "foo")    {
     /* assuming that "someInput" is a jQuery object since you are using "show()"*/
        /* use val() since someINput is jQuery object... can chain the methods*/

      someInput1.show().val("Some Foo text"); 

    }
})
相关问题