0

我正在努力使用提交按钮,单击该按钮时没有任何反应。我相信问题是没有editvalidation()被触发,但我不确定。如果是这种情况,如何触发?我希望验证显示在它自己的提交按钮上方。我的问题只是如何让提交按钮做某事?

我没有收到任何错误:

代码:

<script type="text/javascript">

            function editvalidation()
    {
        if ($("#coursesDrop").val()==""){
            $("#courseAlert").html("Please Select a Course from the Course Drop Down Menu");
        }
        if ($("#coursesDrop").val()!="" && $("#courseadd").children().length==0){
            $("#courseAlert").html("You have not Selected any Students you wish to Add into Course");
        }
        if ($("#coursesDrop").val()!="" && $("#courseadd").children().length!=0){
            return true;
        }
        return false;
    }

            </script>


    <div id='lt-container'>
    <form action='addstudentcourse.php' method='post' id='courseForm'>
    <p id='warnings'></p>
    <p><strong>Courses:</strong> <select name="course" id="coursesDrop">
    <option value="">Please Select</option>
    <option value='19'>BIO234 - Biology</option>
    <option value='1'>INFO101 - Bsc Information Communication Technology</option>
    <option value='2'>INFO102 - Bsc Computing</option>

    </select> </p>   
    </form>
    <form id='updateForm'>

        <p><strong>Course Chosen:</strong></p>
        <table>
        <tr>
        <th></th>
        <td><input type='hidden' id='currentCourseId' name='CourseIdcurrent' value='' /> </td>
        </tr>
        <tr>
        <th>Course ID:</th>
        <td><input type='text' id='currentCourse' name='Coursecurrent' readonly='readonly' value='' /> </td>
        </tr>
        <tr>
        <th>Course Name:</th>
        <td><input type='text' id='currentCourseName' name='CourseNamecurrent' readonly='readonly' value='' /> </td>
        </tr>
        <tr>
        <th>Duration (Years):</th>
        <td><input type='text' id='currentDuration' name='Durationcurrent' readonly='readonly' value=''/> </td>
        </tr>
        </table>
        </form>

    <form id='studentExistForm'>
    <p><strong>Current Students in Chosen Course:</strong></p>
    <p><select name="studenttextarea" id="studentselect" size="10"></select></p>
    </form> 

    </div>

    <div id='rt-container'>
    <form id='studentRemainForm'>
    <p><strong>Available Students to Enrol:</strong></p>
    <p><select multiple="multiple" name="remaintextarea" id="studentremain" size="10"></select></p>
    <table id='addtbl'>
    <tr>
    </table>
    </form>

    <form id='studentAddForm'>
    <p><strong>Students to Add to Course:</strong></p>
    <p><select multiple="multiple" name="addtextarea" id="studentadd" size="10"></select></p>
    <table id='removetbl'>
    <tr>
    </table>

    <div id='studentAlert'></div>
    </form>

    <p><button id='submitstudents'>Submit Students</button></p>

    </div>

        <script type="text/javascript">    



                 function showConfirm(){

                  var courseNoInput = document.getElementById('currentCourse').value;
                  var courseNameInput = document.getElementById('currentCourseName').value;

                  if (editvalidation()) {

                 var confirmMsg=confirm("Are you sure you want to add your selected Students to this Course:" + "\n" + "Course: " + courseNoInput + " - " + courseNameInput);

                 if (confirmMsg==true)
                 {
                 submitform();   
             }
          }
        } 

         $('body').on('click', '#submitstudents', showConfirm);     


        </script>
4

3 回答 3

0
  if (editvalidation()  === true) {
       //your code
     }
 compare function true or false like this ..


$( '#studentAddForm' ).submit();

最好提交这样的表格..

于 2013-03-01T13:20:18.960 回答
0

脚本开始在带有 jquery 1.8.1 的 Google Chrome 中运行。一个问题是,没有定义 ID 为 courseAlert 的元素。

于 2013-03-01T13:26:30.270 回答
0

我格式化了您的代码并对其进行了修复,以便提交按钮正常工作。

我还在每个函数的开头添加了警报,以便您可以看到它们被调用。

如果您希望脚本完全正常工作,您还需要更正其他代码。

链接到工作示例: http ://simplestudio.rs/yard/submit_button/form.html

代码:

<html>
<head>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script>

$(document).ready(function() {

    $("#submitstudents").click(function() { 
        showConfirm();
    }); 

}); 

function showConfirm() {

    alert("function showConfirm is called");
    var courseNoInput = document.getElementById('currentCourse').value;
    var courseNameInput = document.getElementById('currentCourseName').value;

    if (editvalidation()) {

        var confirmMsg=confirm("Are you sure you want to add your selected Students to this Course:" + "\n" + "Course: " + courseNoInput + " - " + courseNameInput);

        if (confirmMsg==true) {

            submitform();   

        }

    }

} 

function editvalidation() {

    alert("function editvalidation is called");
    if ($("#coursesDrop").val()==""){

        $("#courseAlert").html("Please Select a Course from the Course Drop Down Menu");

    }
    if ($("#coursesDrop").val()!="" && $("#courseadd").children().length==0){

        $("#courseAlert").html("You have not Selected any Students you wish to Add into Course");

    }
    if ($("#coursesDrop").val()!="" && $("#courseadd").children().length!=0){

        return true;

    }

    return false;

}


</script>

<style>

    span {
        cursor: pointer;
        position: absolute;
    }

    #divtoshow {
        position: absolute;
        display: none;
    }

</style>

</head>

<body>

    <div id='lt-container'>

        <form action='addstudentcourse.php' method='post' id='courseForm'>

            <p id='warnings'></p>
            <p>

                <strong>Courses:</strong> 

                <select name="course" id="coursesDrop">
                    <option value="">Please Select</option>
                    <option value='19'>BIO234 - Biology</option>
                    <option value='1'>INFO101 - Bsc Information Communication Technology</option>
                    <option value='2'>INFO102 - Bsc Computing</option>
                </select> 

            </p>   

        </form>

        <form id='updateForm'>

            <p>
                <strong>Course Chosen:</strong>
            </p>

            <table>
                <tr>

                    <th></th>
                    <td>
                        <input type='hidden' id='currentCourseId' name='CourseIdcurrent' value='' /> 
                    </td>

                </tr>

                <tr>

                    <th>
                        Course ID:
                    </th>
                    <td>
                        <input type='text' id='currentCourse' name='Coursecurrent' readonly='readonly' value='' /> 
                    </td>

                </tr>

                <tr>

                    <th>
                        Course Name:
                    </th>

                    <td>
                        <input type='text' id='currentCourseName' name='CourseNamecurrent' readonly='readonly' value='' /> 
                    </td>

                </tr>

                <tr>

                    <th>
                        Duration (Years):
                    </th>

                    <td>
                        <input type='text' id='currentDuration' name='Durationcurrent' readonly='readonly' value=''/> 
                    </td>

                </tr>

            </table>

        </form>

        <form id='studentExistForm'>

            <p>
                <strong>Current Students in Chosen Course:</strong>
            </p>

            <p>
                <select name="studenttextarea" id="studentselect" size="10"></select>
            </p>

        </form> 

    </div>

    <div id='rt-container'>

        <form id='studentRemainForm'>

            <p>
                <strong>Available Students to Enrol:</strong>
            </p>

            <p>
                <select multiple="multiple" name="remaintextarea" id="studentremain" size="10"></select>
            </p>

            <table id='addtbl'>
                <tr>
            </table>

        </form>

        <form id='studentAddForm'>

            <p>
                <strong>Students to Add to Course:</strong></p>
            </p>

            <p>
                <select multiple="multiple" name="addtextarea" id="studentadd" size="10"></select>
            </p>

            <table id='removetbl'>
                <tr>
            </table>

            <div id='studentAlert'></div>

        </form>

        <p>
            <button id='submitstudents'>Submit Students</button>
        </p>

    </div>

</body>
</html>
于 2013-03-01T13:26:39.817 回答