简体   繁体   English

在 html.erb 中的 js 函数中使用 js 函数

[英]using js functions within js functions in html.erb

ok should be an easy one for everyone,... i am calling a javascript function in the tag of a button using inclick.好的应该对每个人来说都很容易,...我正在使用inclick在按钮的标签中调用javascript function。 Im trying to get that function to have three different parameters.我试图让 function 具有三个不同的参数。 The function then submits three different times, which should end up being three different records in a ruby table. function 然后提交三个不同的时间,这最终应该是 ruby 表中的三个不同的记录。 But i cant see why this doesnt work...但我不明白为什么这不起作用......

<script>
function submiteffort( elem )
{
    // Elem 1
    $("#effort_hours").val( $( elem ).val() );
    $("#task_id").val( elem.id );
    $("#effort_form").submit();
    return true;  
}

function medium( leave, toil, sick)
{
    var dave = submiteffort(document.getElementsByName(leave));

    if(dave == true){
        var dave2 = submiteffort(document.getElementsByName(toil));
    }

    if(dave2 == true){
        submiteffort(document.getElementsByName(sick));
    }
}
</script>

<div class="startleft">
    <table>
        <tr>
            <td>Leave</td>
            <td><input class="dayinput" type="text" name="Leave" placeholder="0" ></td>
        </t>
        <tr>
            <td>TOIL</td>
            <td><input class="dayinput" type="text" name="TOIL" placeholder="0"></td>
        </tr>
        <tr>
            <td>Sick</td>
            <td><input class="dayinput" type="text" name="Sick" placeholder="0"></td>
        </tr>
        <tr>
            <td>Total</td>
            <td><input id="total" class="total_low" type="text" value="0" disabled="" name="Dave">
        </tr>
        <tr>
            <td></td>
        <td><button onclick="medium('Leave','TOIL','Sick')">Commit</button></td>
        </tr>
    </table>
    </div>

For some reason this only submits 1 record into the table and i cant figure out why.由于某种原因,这只会将 1 条记录提交到表中,我不知道为什么。

Well if you submit the form, the page refreshes, and the other 2 function calls don't execute.好吧,如果您提交表单,页面刷新,并且其他 2 个 function 调用不执行。 You'd have to use AJAX to send data to the backend in 3 separate function calls.您必须使用 AJAX 在 3 个单独的 function 调用中将数据发送到后端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM