使用带有jquery的data-attribute获取输入值

时间:2022-04-01 18:57:15

How can i get the input value using data-attributes with jQuery? This is my function which is not working. the return value of valuefromscenariotwo should be "My test 2"

如何使用jQuery的数据属性获取输入值?这是我的功能无法正常工作。 valuefromcenariotwo的返回值应为“我的测试2”

var valuefromscenariotwo = getvaluefromScenariotwo("scenario2");
function getvaluefromScenariotwo(name){
  return $(input[data-scenario=name]).val();
}

<div class="form-group">
<input type="text" data-scenario="scenario1" class="form-control notes" placeholder="Enter Notes"           value="My test 1">
</div>;
<div class="form-group">
<input type="text" data-scenario="scenario2" class="form-control notes" placeholder="Enter Notes"   value="My test 2">
</div>;
<div class="form-group">
<input type="text" data-scenario="scenario3" class="form-control notes" placeholder="Enter Notes"   value="My test 3">
</div>;

1 个解决方案

#1


6  

You need to add some quotes and concatenation to that

您需要添加一些引号和连接

function getvaluefromScenariotwo(name){
    return $('input[data-scenario="' + name + '"]').val();
}

#1


6  

You need to add some quotes and concatenation to that

您需要添加一些引号和连接

function getvaluefromScenariotwo(name){
    return $('input[data-scenario="' + name + '"]').val();
}