如何从数据选择的输入选项中获取值?

时间:2016-06-01 19:47:38

标签: javascript

如何从此输入中获取数据选择的值?我们还说我只想要那个“头衔”值吗?

$( document ).ready(function() {
	//console.log("prdcl ready");
	$("#digital_object_prdcls__0__prdcl_links__0__ref_").on('keyup change', prdcl_link);
	$("#digital_object_prdcls__0__volume_num_").on('keyup change', prdcl_link);
	$("#digital_object_prdcls__0__issue_num_").on('keyup change', prdcl_link);

    function prdcl_link(){
		//console.log("entered function");
		var valp = {};
		valp = $("#digital_object_prdcls__0__prdcl_links__0__ref_").data('data-selected');
		console.log(valp);
        var valv = $("#digital_object_prdcls__0__volume_num_").val();
		//console.log(valv);
        var vali = $("#digital_object_prdcls__0__issue_num_").val();
		//console.log(vali);

   };
});
<input type="text" class="linker initialised" id="digital_object_prdcls__0__prdcl_links__0__ref_" data-label="Publication" data-label_plural="Publications" data-name="ref" data-path="digital_object[prdcls][0][prdcl_links][0]" data-url="/search.json" data-browse-url="/search.js?" data-selected="{"lock_version":0,"publication":"1111","publisher":"1111","title":"1111 (1111)","created_by":"admin","last_modified_by":"admin","create_time":"2016-06-01T14:46:46Z","system_mtime":"2016-06-01T14:46:46Z","user_mtime":"2016-06-01T14:46:46Z","jsonmodel_type":"prdcl_title","uri":"/prdcl_titles/9"}" data-multiplicity="one" data-format_template="title" data-types="["prdcl_title"]" style="display: none;">

2 个答案:

答案 0 :(得分:2)

不要在代码中使用属性的第一个data-块:

valp = $("#digital_object_prdcls__0__prdcl_links__0__ref_").data('selected');

来源:http://api.jquery.com/data/#data-html5

答案 1 :(得分:2)

因为您使用双引号来构造data-selected内的对象,所以会得到无效的标记:data-selected变为“{”。

修复标记(例如使用单引号):

data-selected='{"lock_version":0,"publication":"1111","publisher":"1111","title":"1111 (1111)","created_by":"admin","last_modified_by":"admin","create_time":"2016-06-01T14:46:46Z","system_mtime":"2016-06-01T14:46:46Z","user_mtime":"2016-06-01T14:46:46Z","jsonmodel_type":"prdcl_title","uri":"/prdcl_titles/9"}'

使用以下方法检索data-selected

$('#digital_object_prdcls__0__prdcl_links__0__ref_').data('selected')