iT邦幫忙

0

想請問這功能我該如何做判斷

  • 分享至 

  • xImage

https://ithelp.ithome.com.tw/questions/10192904

這是我前幾天問的問題,這第一步功能已經實現出來,接下來打算追加一個新的
textboxarea

https://ithelp.ithome.com.tw/upload/images/20190319/20110132hVRJgNuUtM.jpg

目前是按下按鈕兩個textboxarea 都會帶入值,我該如何判斷游標在哪才會把值帶入
游標所在的textbox裡?

這是我目前的程式碼
HTML

<div class="row">
    <div class="col-md-12">
        <div class="box box-primary">
            <div class="box-header">
                <h3 class="box-title">標點符號提示欄</h3> <div class="result-number"> </div>
            </div><!-- /.box-header -->
            <div class="box-body">
                <button type="button" class="btn btn-primary textBtn">0</button>
                <button type="button" class="btn btn-primary textBtn">1</button>
                <button type="button" class="btn btn-primary textBtn">2</button>
                <button type="button" class="btn btn-primary textBtn">3</button>
                <button type="button" class="btn btn-primary textBtn">4</button>
                <button type="button" class="btn btn-primary textBtn">5</button>
                <button type="button" class="btn btn-primary textBtn">6</button>
                <button type="button" class="btn btn-primary textBtn">7</button>
                <button type="button" class="btn btn-primary textBtn">8</button>
                <button type="button" class="btn btn-primary textBtn">9</button>
                <button type="button" class="btn btn-primary textBtn">、</button>
                <button type="button" class="btn btn-primary textBtn">,</button>
                <button type="button" class="btn btn-primary textBtn">。</button>
                <button type="button" class="btn btn-primary textBtn">:</button>
                <button type="button" class="btn btn-primary textBtn">#</button>
                <button type="button" class="btn btn-primary textBtn">*</button>
                <button type="button" class="btn btn-primary textBtn">%</button>
                <button type="button" class="btn btn-primary textBtn">/</button>
                <button type="button" class="btn btn-primary textBtn">(</button>
                <button type="button" class="btn btn-primary textBtn">)</button>
                <button type="button" class="btn btn-primary textBtn">°C</button>
                <button type="button" class="btn btn-primary textBtn">Kg</button>
                <button type="button" class="btn btn-primary textBtn">Cm</button>
                <button type="button" class="btn btn-primary textBtn">mmHg</button>
                <button type="button" class="btn btn-primary textBtn">g</button>
                <button type="button" class="btn btn-primary textBtn">ug</button>
                <button type="button" class="btn btn-primary textBtn">mg</button>
                <p></p>
                <button type="button" class="btn btn-primary textBtn">mL</button>
                <button type="button" class="btn btn-primary textBtn">vial</button>
                <button type="button" class="btn btn-primary textBtn">amp</button>
                <button type="button" class="btn btn-primary textBtn">mg/dl</button>
                <button type="button" class="btn btn-primary textBtn">mmol/l</button>
            </div>
        </div>
        <!-- /.box -->
    </div>
    <!-- /.col -->
</div>
<div class="row">
    <div class="col-md-12">
        @using (Html.BeginForm("AddOrUpdate", "HDNurseRecord", new { area = "HDNurse" }, FormMethod.Post, new { @class = "form-inline" }))
        {
            @Html.HiddenFor(m => m.PatScheduleGuid)
            @Html.HiddenFor(m => m.GUID)
            @Html.HiddenFor(m => m.CID)
            @Html.HiddenFor(m => m.PAT_GUID)
            @Html.HiddenFor(m => m.NURSE_EMP_ID)
            @Html.HiddenFor(m => m.CRE_UID)
            @Html.HiddenFor(m => m.MOD_UID)

            <div class="box box-primary">
                <div class="box-header">
                    <h3 class="box-title">護理紀錄</h3>
                    <button type="submit" class="btn btn-primary pull-right">儲存</button>
                    <button type="button" class="fn-back btn btn-default pull-right btn-right-margin">取消</button>
                    @if (EditMode == Constants.EditMode_Edit)
                    {
                        // 新增模式不會有刪除按鈕
                        <button type="button" class="fn-delete btn btn-default pull-right btn-right-margin"
                                data-uid="@Model.GUID" data-tip="">
                            刪除
                        </button>
                    }
                </div><!-- /.box-header -->
                <div class="box-body">
                    <div class="form-flex-container">
                        <div class="row-flex">
                            <div class="col-label">
                                <label class="control-label">紀錄日期</label>
                            </div>
                            <div class="col-input">
                                @Html.TextBoxFor(m => m.RecordDateString, new { @class = "form-control datepicker" })
                                @Html.TextBoxFor(m => m.RecordTimeString, new { @class = "form-control timepicker" })
                            </div>
                        </div><!-- /.row -->
                        <div class="row-flex">
                            <div class="col-label">
                                <label class="control-label">治療/處置</label>
                            </div>
                            <div class="col-input">
                                @Html.TextAreaFor(m => m.ORDER_CONTENT, new { @class = "form-control", @rows = "3", @style = "width:80%;",@id="OrderContent" })
                            </div>
                        </div><!-- /.row -->
                        <div class="row-flex">
                            <div class="col-label">
                                <label class="control-label">
                                    紀錄內容
                                    <button type="button" name="AddRecrod" data-content="@Model.CONTENT" class="fn-phrase btn btn-default" data-toggle="modal" data-target="#Phrase_Dialog">片語</button>      
                                </label>
                            </div>
                            <div class="col-input">
                                @Html.TextAreaFor(m => m.CONTENT, new { @class = "form-control", @rows = "8", @style = "width:80%;", @id = "DescContest",})
                            </div>
                        </div><!-- /.row -->
                    </div><!-- /.form-flex-container -->
                </div><!-- /.box-body -->
            </div><!-- /.box -->
        }<!-- /.form -->
    </div><!-- /.col -->
</div><!-- row -->

JQUERY

$(function () {
 $('.textBtn').click(function () {
            var text = $(this).text()
        
            $('#DescContest').insertAtCaret(text);
            //$('#OrderContent').insertAtCaret(text);
        })

        //依游標所在處插入文字↓↓
        $.fn.extend({
            insertAtCaret: function (myValue) {
                this.each(function () {

                    if (document.selection) {
                        //IE用
                        this.focus();
                        var sel = document.selection.createRange();
                        sel.text = myValue;
                        this.focus();
                    } else if (this.selectionStart || this.selectionStart == '0') {
                        //其他瀏覽器用
                        var startPos = this.selectionStart;
                        var endPos = this.selectionEnd;
                        var scrollTop = this.scrollTop;
                        this.value = this.value.substring(0, startPos) +
                          myValue + this.value.substring(endPos, this.value.length);
                        this.focus();
                        this.selectionStart = startPos + myValue.length;
                        this.selectionEnd = startPos + myValue.length;
                        this.scrollTop = scrollTop;
                    } else {
                        this.value += myValue;
                        this.focus();
                    }
                });
                return this;
            }
        });
  })
Han iT邦研究生 1 級 ‧ 2019-03-19 17:27:15 檢舉
你是指
哪一個textarea是在focus的狀況下 去按按鈕
然後要在那個textare觸發新增字的狀況嘛??
tenno081 iT邦研究生 4 級 ‧ 2019-03-20 09:12:29 檢舉
差不多,比如我在紀錄內容這裡打字,然後按下按鈕,希望按鈕的值是在

紀錄內容裡,而上面的治療/處置不會跟著有值。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
froce
iT邦大師 1 級 ‧ 2019-03-19 19:01:07
最佳解答
tenno081 iT邦研究生 4 級 ‧ 2019-03-20 09:49:12 檢舉

感謝大大,成功做出,那想請問我下面那段依游標所在處插入文字

是真的用不到了嗎?

froce iT邦大師 1 級 ‧ 2019-03-20 11:27:06 檢舉

你自己測試看需不需要吧。

我要發表回答

立即登入回答