在onclick上按类获取此值

时间:2016-10-31 12:08:00

标签: javascript jquery html

我有一个表格和jquery,用于按类

获取值this



function sendModel(el){
var id_model = $(el).class('id_model');
  alert(id_model);
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input name='id_model' class='id_model' value='M001'>
  
  <button onclick='sendModel(this)'> send </button>
  
  </form>
&#13;
&#13;
&#13;

如何用id_model值显示警告?谢谢你们

2 个答案:

答案 0 :(得分:1)

提交表格

$('#myForm').submit(function(e){
  e.preventDefault();
  var id_model = $('.id_model').val();
  alert(id_model);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myForm">
  <input name='id_model' class='id_model' value='M001'>
  
  <button type="submit"> send </button>
  
  </form>

你的方式

function sendModel(){
  var id_model = $('.id_model').val();
  alert(id_model);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input name='id_model' class='id_model' value='M001'>
  
  <button onclick='sendModel()'> send </button>
  
  </form>

答案 1 :(得分:0)

你可以尝试一下吗?

var id_model = $('.id_model');

alert(id_model.attr('value'))