如何将此jquery转换为javascript

时间:2014-06-24 02:50:15

标签: javascript

嗨,我不能在这里使用Jquery。请帮我把这3个选择器改成javascript。 我已经尝试过document.getElementById('FeedbackLightBox_txtName')。value;但我不行。

    <script type="text/javascript">
    function SetButtonStatus() {
        var tb1 = document.getElementById('FeedbackLightBox_txtName').value;
        var tb2 = document.getElementById('FeedbackLightBox_txtMessage').value;
        var tb3 = document.getElementById('FeedbackLightBox_txtEmail').value;
        if (tb1.length >= 5 && tb2.length >= 5 && tb3.length >= 5)
            makeBtn();
        else
        $('#FeedbackLightBox_btnSubmit')[0].control.set_enabled(false);
    }
    function makeBtn() {
        $('#FeedbackLightBox_btnSubmit')[0].control.set_enabled(true);
    }
    function ClearValues(sender, args) {
        $('#FeedbackLightBox_txtName').val('');
        $('#FeedbackLightBox_txtMessage').val('');
        $('#FeedbackLightBox_txtEmail').val('');
        args.set_cancel(true);
        return false;
    }
</script>

4 个答案:

答案 0 :(得分:0)

喜欢这个吗?

document.getElementById('FeedbackLightBox_btnSubmit').control.set_enabled(false);

document.getElementById('FeedbackLightBox_txtName').value = '';

<强> JFYI

当你说

$('#FeedbackLightBox_btnSubmit')[0] // it gives you an access to HTML element object which is equivalent to saying `document.getElementById('Feed..)`.  

答案 1 :(得分:0)

不确定,在元素上定义了.control.set_enabled(false);,但我猜你确实知道。

function SetButtonStatus() {
        var tb1 = document.getElementById('FeedbackLightBox_txtName').value;
        var tb2 = document.getElementById('FeedbackLightBox_txtMessage').value;
        var tb3 = document.getElementById('FeedbackLightBox_txtEmail').value;
        if (tb1.length >= 5 && tb2.length >= 5 && tb3.length >= 5) {
                makeBtn();
        } else {
            document.getElementById("FeedbackLightBox_btnSubmit").control.set_enabled(false);
        }
}
function makeBtn() {
        document.getElementById("FeedbackLightBox_btnSubmit").control.set_enabled(true);
}
function ClearValues(sender, args) {
        document.getElementById("FeedbackLightBox_txtName").value = '';
        document.getElementById("FeedbackLightBox_txtMessage").value = '';
        document.getElementById("FeedbackLightBox_txtEmail").value = '';
        args.set_cancel(true);
        return false;
}

答案 2 :(得分:0)

似乎你有一个类似的形式:

<form id="FeedbackLightBox_form" onsubmit="return validate(this)" action="">
  Name: <input name="FeedbackLightBox_txtName"><br>
  Message: <teaxtArea name="FeedbackLightBox_txtMessage"></textarea><br>
  Email: <input name="FeedbackLightBox_txtEmail"><br>
  <input type="submit" id="FeedbackLightBox_btnSubmit">  <input type="reset">
</form>  
在提交之前,您想检查某些内容已经输入到每个字段中,因此您可以使用以下方法对其进行验证:

function validate(form) {
  var control, controls = form.controls;
  for (var i=0, iLen=controls.length; i<iLen; i++) {
    control = controls[i];
    if (control.type != 'submit' && control.value.length < 5) {
      return false;
    }
  }
}

如果你包含一个重置按钮,那么 clearForm 功能肯定是不必要的,但无论如何:

function clearForm() {
  document.getElementById('FeedbackLightBox_form').reset();
}

同样,(不恰当命名的) makeBtn 函数可以是:

function makeBtn() {
    document.betElementById('FeedbackLightBox_btnSubmit').disabled = false;
}

也许它应该被称为 enableBtn

答案 3 :(得分:0)

看起来jQuery仅用于获取HTML元素,因此您可以在函数之前将它们定义为全局变量(或将它们作为函数参数传递)以避免重复。

var tb1 = document.getElementById('FeedbackLightBox_txtName');
var tb2 = document.getElementById('FeedbackLightBox_txtMessage');
var tb3 = document.getElementById('FeedbackLightBox_txtEmail');
var btn = document.getElementById('FeedbackLightBox_btnSubmit');

function SetButtonStatus() {
    if (tb1.value.length >= 5 && tb2.value.length >= 5 && tb3.value.length >= 5)
        makeBtn();
    else
        btn.control.set_enabled(false);
}
function makeBtn() {
    btn.control.set_enabled(true);
}
function ClearValues(sender, args) {
    tb1.value = '';
    tb2.value = '';
    tb3.value = '';
    args.set_cancel(true);
    return false;
}