javaScript代码无法正常工作

时间:2015-08-31 05:17:14

标签: javascript html

<script type="text/javascript">
var x = 0.025399;

function calculatebmi() {
    var weight = +document.bmiform1.weight.value;
    var feet = +document.bmiform.feet.value;
    var inches = +document.bmiform.inches.value;
    inches = 12 * feet + inches;
    var height = x * inches;
    var finalbmi = weight / (height * height);
    document.bmiform3.bmi.value = finalbmi;
}

function validate() {
    var age = document.ageForm.age.value;
    var feet = document.bmiform.feet.value;
    var inches = document.bmiform.inches.value;
    var weight = document.bmiform1.weight.value;

    if (age == "" || feet == "" || inches == "" || weight == "") {
        alert("Your fields are empty");
    }
}
</script>

我的HTML是

<tr>
    <td>
        <h4>Enter Your Age</h4></td>
    <td>
        <form name="ageForm">
            <input type="text" name="age" size="5px" />AGE
        </form>
    </td>
</tr>
<tr>
    <td>
        <h4>Enter your height</h4></td>
    <td>
        <form name="bmiform">
            <input type="text" name="feet" size="5px" /> FEET
            <input type="text" name="inches" size="5px" /> inches
        </form>
    </td>
</tr>
<tr>
    <td>
        <h4>Enter your weight</h4></td>
    <td>
        <form name="bmiform1">
            <input type="text" name="weight" size="5px" /> KG
        </form>
    </td>
</tr>
<tr>
    <td colspan="2">
        <form name="bmiform3">
            Your BMI:
            <input type="text" name="bmi" />
            <input class="button1" type="button" name="calculate" value="Calculate" title="calculate" onclick="calculatebmi()" />

        </form>
    </td>

当我没有填写年龄,体重,身高字段时,它没有按照我告诉它使用脚本来提示任何警报框?任何人都可以帮助我找出这里出了什么问题,任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

在进行任何处理之前,您需要在calculatebmi中调用validate函数。

ComboBox ctl = e.Control as ComboBox;
ctl.Enter -= new EventHandler(ctl_Enter);
ctl.Enter += new EventHandler(ctl_Enter);

答案 1 :(得分:1)

在代码中进行以下更改以使其正常工作。

(function( $ ) {

var proto = $.ui.autocomplete.prototype,
    initSource = proto._initSource;

function filter( array, term ) {
    var matcher = new RegExp( $.ui.autocomplete.escapeRegex(term), "i" );
    return $.grep( array, function(value) {
        return matcher.test( $( "<div>" ).html( value.label || value.value || value ).text() );
    });
}

$.extend( proto, {
    _initSource: function() {
        if ( this.options.html && $.isArray(this.options.source) ) {
            this.source = function( request, response ) {
                response( filter( this.options.source, request.term ) );
            };
        } else {
            initSource.call( this );
        }
    },

    _renderItem: function( ul, item) {
        return $( "<li></li>" )
            .data( "item.autocomplete", item )
            .append( $( "<a></a>" )[ this.options.html ? "html" : "text" ]( item.label ) )
            .appendTo( ul );
    }
});

})( jQuery );

$('#recherche').autocomplete({
        source: [
            {
                label: "aardvark", 
                value: "aardvark"
            },
            {
                label: "<b>apple</b>",
                value: "apple"
            },
            {
                label: "<i>atom</i>",
                value: "atom"
            }
        ],
        html: true
    });
相关问题