Javascript返回undefined

时间:2013-04-20 22:39:22

标签: javascript jquery

我试图获取微调器的值并显示其值。这是微调代码:

<div id="site_content">
    <div id="content">
        <h:body>
            <h:form>
                <div class="whiteSpace" />
                <p:spinner id="minutes" min="0" max="1000" value="#{printerSettings.t}" size="1">
                    <p:ajax update="NewTime" />
                    <f:validateLongRange minimum="1" maximum="1000" />
                </p:spinner>
                <h:message id="minutes2" for="minutes" style="color:red" />

这是当用户按下提交时调用的javascript代码

function Thankyou() {
    var minuteVal = $('minutes').val(); //not finding the minutes selected
    alert(minuteVal);
    alert("Sent to the printing holding queue, you may close this app now or carry on using this app, your work will still print out ");
    //location.href = 'index.xhtml';
}

我试过了

var minuteVal = $('minutes').text();

然后返回一个空框。感谢。

修改

找到上面的JavaScript

                </h:form>
            </h:body>
        </div>
    </div>
    <script type="text/javascript">
        .....
    </script>
</html>

位于XHTML的底部。

修改

这里是来源,我不知道这意味着什么

In how many minutes time would you like to have your job sent to the printer ?
<div class="whiteSpace"></div>
<span id="j_idt8:minutes" class="ui-spinner ui-widget ui-corner-all">
    <input id="j_idt8:minutes_input" name="j_idt8:minutes_input" type="text" class="ui-spinner-input ui-inputfield ui-state-default ui-corner-all" autocomplete="off" value="1" size="1" />
    <a class="ui-spinner-button ui-spinner-up ui-corner-tr ui-button ui-widget ui-state-default ui-button-text-only">
        <span class="ui-button-text">
            <span class="ui-icon ui-icon-triangle-1-n"></span>
        </span>
    </a>
    <a class="ui-spinner-button ui-spinner-down ui-corner-br ui-button ui-widget ui-state-default ui-button-text-only">
        <span class="ui-button-text">
            <span class="ui-icon ui-icon-triangle-1-s"></span>
        </span>
    </a>
</span>
<script id="j_idt8:minutes_s" type="text/javascript">
    PrimeFaces.cw('Spinner', 'widget_j_idt8_minutes', {
        id: 'j_idt8:minutes',
        step: 1.0,
        min: 0.0,
        max: 1000.0,
        behaviors: {
            change: function(event) {
                PrimeFaces.ab({
                    source: 'j_idt8:minutes',
                    event: 'valueChange',
                    process: 'j_idt8:minutes',
                    update: 'j_idt8:NewTime'
                }, arguments[1]);
            }
        }
    });
</script>
<span id="j_idt8:minutes2"></span>

修改

function Thankyou()
{
    var minutesVal = $('#minutes').spinner('value');//not finding the minutes selected
    alert(minutesVal);
    alert("Sent to the printing holding queue, you may close this app now or carry on using this app, your work will still print out ");
    //location.href = 'index.xhtml';
}

现在没有任何反应,鼠标上的加载符号出现但没有消息框等

3 个答案:

答案 0 :(得分:4)

选择器'minutes'会查找标记名为'minutes'的元素,如<minutes>中所示。你想通过id,'#minutes'

var minuteVal = $('#minutes').val();

答案 1 :(得分:3)

jQuery基于css样式的选择器选择元素;例如,您目前拥有的是选择'分钟'标签,您需要的是:

 var minuteVal = $('#minutes').attr("value");

除此之外,value不是除inputtextareaselect标记之外的任何有效属性,因此您需要使用{{1带有data-的非标准html属性的前缀属性,或者做类似于上面的操作。

答案 2 :(得分:2)

从HTML中的最终源抓取第一个和第二个微调器:

var firstSpinner, secondSpinner;
$('input.ui-spinner-input').each(function(index) {
    if(index == 0) {firstSpinner = $(this).val();}
    else if (index == 1) {secondSpinner = $(this).val();} 
});
alert('First spinner: ' + firstSpinner + ' Second spinner: ' + secondSpinner);