根据body类值的值填充字段

时间:2016-06-02 13:22:35

标签: javascript jquery html

我正在尝试使用body元素中的一个类来填充表单中的字段,该元素开始于" pageid - "然后有一个数字。我想找到这个数字,并将其添加为表单值,这使我能够在每个页面上使用相同的表单,但确定表单提交的页面。

我还在学习Java / JQuery,因此不知道如何在这里返回值。它以[object Object]的形式进入 - 这就是我目前所拥有的:

HTML

<body class="pageid-1232">

<input type="text" id="test1" value="">

</body>

的Javascript

var thisid = $('[class^="pageid-"]');

$(document).ready(function () {
    $("#test1").val( thisid );
});

http://jsfiddle.net/Gg6AG/44/

2 个答案:

答案 0 :(得分:2)

我建议:

$(document).ready(function(){
    // converts the classList - an Array-like list of class-names - of
    // of the <body> element/node into an Array; and then filters that
    // Array of class-names using Array.filter.prototype(), to retain
    // only those classes for which the inner comparison return true:
    var classValue = Array.from( document.body.classList ).filter(function(c){
        // 'c' (the first argument of the method) is the
        // array-element of the Array over which we're iterating
        // and is supplied by the Array method.

          // here we return only those class-names that have
          // a string of numbers ('\d+') before the end of
          // string ('$'), using RegExp.test() to return
          // a Boolean, true: the string matches the regexp,
          //            false: the string does not match:
                    return /\d+$/.test(c);

                // here we assume only one matching class-name,
                // and user Array.prototype.join('') to convert
                // that single-element array into a String 
                // and split that class-name on the '-' character:
                }).join('').split('-')
                // and use Array.prototype.pop() to retrieve
                // the last element of the returned Array:
                .pop();

    // finding the element with the id 'test1' and setting
    // its value property to the found-value:
    document.getElementById('test1').value = classValue;
});

JS Fiddle demo

顺便说一下这个原因:

$("#test1").val( thisid );

无法工作是因为thisid是一个jQuery对象,当你尝试通过传递一个Object(一个Object,而不是一个object-property)设置一个字符串值时,JavaScript引擎会尝试将Object强制转换为String表示形式,这将生成[object Object]的值,可以通过以下方式验证:

console.log({}.toString());

&#13;
&#13;
var obj = {};

document.querySelector('input').value = obj;

console.log(obj.toString());
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input />
&#13;
&#13;
&#13;

或进行更为比较的演示:

console.log($('body'));

&#13;
&#13;
var obj = $('body');

document.querySelector('input').value = obj;

console.log(obj.toString());
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input />
&#13;
&#13;
&#13;

答案 1 :(得分:0)

试试这个:

var thisid = $('[class^="pageid-"]');

$(document).ready(function () {
    var num = $(thisid).attr("class").split("-").pop(); // it will extract the number from class 
    $("#test1").val( num ); // set that number to input field
});